package cn.xmy.demo04.controller;


import cn.xmy.demo04.mapper.HouseInfoMapper;
import cn.xmy.demo04.mapper.HouseTypeMapper;
import cn.xmy.demo04.pojo.HouseInfo;
import cn.xmy.demo04.pojo.HouseType;
import cn.xmy.demo04.service.HouseInfoService;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import java.util.Collection;

@Controller
public class HouseController {
    @Autowired
    HouseInfoMapper houseInfoMapper;
    @Autowired
    HouseTypeMapper houseTypeMapper;
    @Autowired
    HouseInfoService houseInfoService;
    //查询
    @RequestMapping("emps")
    public String list(Model model){
        Collection<HouseInfo> houseInfos = houseInfoMapper.selectAllHouseInfo();
        model.addAttribute("houseInfos",houseInfos);
        return "index";
    }
    // 分页查询全部
    @RequestMapping(value = "/houseManager/list")
    public String getHouseList(Model model, HttpSession session,
                               @RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex) {
        PageInfo<HouseInfo> pageInfo = null;
        try {
            pageInfo = houseInfoService.selectPage(pageIndex, 3);
            System.out.println(pageInfo.getList().size());
        } catch (Exception e) {
            e.printStackTrace();
        }

        model.addAttribute("houses", pageInfo.getList());
        model.addAttribute("currentPage", pageInfo.getPageNum());
        model.addAttribute("totalPages", pageInfo.getPages());
        return "index";
    }
    //添加前的查询类型
    @GetMapping("/emp")
    public String toAddEmp(Model model){
        Collection<HouseType> houseTypes = houseTypeMapper.findAll();
        model.addAttribute("houseTypes",houseTypes);
        return "add";
    }
    //添加
    public String add(HouseInfo houseInfo){
        houseInfoMapper.addHouseInfo(houseInfo);
        return "redirect:/houseManager/list";
    }
}
